package store

import (
	"flag"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

type Config struct {
}

type cfg struct {
	Runmode string `yaml:"runmode"`
	Name    string `yaml:"name"`
	Http    struct {
		Port int `yaml:"port"`
	} `yaml:"http"`
	Redis struct {
		Host string `yaml:"host"`
		Port int    `yaml:"port"`
		Pass string `yaml:"password"`
	} `yaml:"redis"`
	Rabbitmq struct {
		Addr string `yaml:"addr"`
	} `yaml:"rabbitmq"`
	Mysql struct {
		Master struct {
			Addr         string `yaml:"addr"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"master"`
		AppRead struct {
			Addr         string `yaml:"addr"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"appRead"`
		AdminRead struct {
			Addr         string `yaml:"addr"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"adminRead"`
	} `yaml:"mysql"`
	Pgsql struct {
		Master struct {
			Addr         string `yaml:"addr"`
			Port         int    `yaml:"port"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"master"`
		AppRead struct {
			Addr         string `yaml:"addr"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"appRead"`
		AdminRead struct {
			Addr         string `yaml:"addr"`
			MaxOpenConns int    `yaml:"maxOpenConns"`
			MaxIdleConns int    `yaml:"maxIdleConns"`
		} `yaml:"adminRead"`
	} `yaml:"pgsql"`
	Grpc struct {
		Listening string `yaml:"listening"`
		Slbs      struct {
			UserServer   string `yaml:"user_server"`
			AuthServer   string `yaml:"auth_server"`
			FileServer   string `yaml:"file_server"`
			WechatServer string `yaml:"wechat_server"`
			MallServer   string `yaml:"mall_server"`
			ToolServer   string `yaml:"tool_server"`
			GdrawServer  string `yaml:"gdraw_server"`
			BlogServer   string `yaml:"blog_server"`
			LogServer    string `yaml:"log_server"`
			MusicServer  string `yaml:"music_server"`
		} `yaml:"slbs"`
	} `yaml:"grpc"`
	Jwt struct {
		AdminSecret  string `yaml:"adminSecret"`
		AdminTtl     int64  `yaml:"adminTtl"`
		ClientSecret string `yaml:"clientSecret"`
		ClientTtl    int64  `yaml:"clientTtl"`
		GdrawSecret  string `yaml:"gdrawSecret"`
		GdrawTtl     int64  `yaml:"gdrawTtl"`
	} `yaml:"jwt"`
	MomoZaHuoPuWechat struct {
		Appid               string `yaml:"appid"`
		Secret              string `yaml:"secret"`
		MchApiKey           string `yaml:"mchApiKey"`
		MchID               string `yaml:"mchId"`
		BillNoticeTemplate  string `yaml:"billNoticeTemplate"`
		BillNoticeUrl       string `yaml:"billNoticeUrl"`
		SystemErrorTemplate string `yaml:"systemErrorTemplate"`
	} `yaml:"momoZaHuoPuWechat"`
	ShortHost    string `yaml:"short_host"`
	GdrawMiniApp struct {
		Appid  string `yaml:"appid"`
		Secret string `yaml:"secret"`
	} `yaml:"gdrawMiniApp"`
}

func (s *Config) Get() (conf cfg) {
	configOnce.Do(func() {
		path := ""
		flag.StringVar(&path, "conf", "./configs/dev.yml", "help")
		flag.Parse()
		bytes, err := ioutil.ReadFile(path)
		if nil != err {
			panic(err)
		}
		err = yaml.Unmarshal(bytes, &conf)
		if nil != err {
			panic(err)
		}
		config = conf
	})
	conf = config
	return
}
